package org.fao.pims.web.pollneeds;


import javax.servlet.http.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.*;
import org.fao.pims.model.*;
import org.fao.pims.service.pollneeds.*;



public class PollinationNeedsAction extends Action {
	
	private static Log log = LogFactory.getLog(PollinationNeedsAction.class);
	
	public ActionForward execute(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception
	{
		
		PollinationNeedsForm pollForm = (PollinationNeedsForm)form;
		
		//String nameOfCrop = pollForm.getNameofcrop();
		String nameOfCrop = pollForm.getScientificName();
		
		log.warn("In Pollination Needs Action");
		
		PollinationNeed pollNeed = null;
		
		PollinationNeedsManager service = new PollinationNeedsManagerImpl();
		
	
		
		try {
		
			pollNeed = service.getPollinationNeed(nameOfCrop);
			
			String commonName = pollNeed.getCrop().getCommonName();
			
			System.out.println("common name in Action: " + commonName);
			
			request.setAttribute("pollneed", pollNeed);						
			
			request.setAttribute("pollinators", pollNeed.getPollinatorList());
			
			request.setAttribute("documents", pollNeed.getSourceList());
			
			System.out.println("general impact: " + pollNeed.getGeneralImpact().trim());
			
			String generalImpact = pollNeed.getGeneralImpact();
			
			if("increase".equals(generalImpact)) {		
				
				return mapping.findForward("increase");
				
			}
			else if("increase seed production".equals(generalImpact)) {
				
				return mapping.findForward("increaseSeedProduction");
				
			}
			else if("no increase".equals(generalImpact)) {
				
				return mapping.findForward("noIncrease");
				
			}
			else if("unknown".equals(generalImpact)) {
				
				return mapping.findForward("unknown");
							
			}
			else if("increase breeding".equals(generalImpact)) {
				
				return mapping.findForward("increaseBreeding");
	
			}
			else {
				
				return mapping.findForward("failure");
				
			}
			
		}
		catch(PollinationNeedsException pne) {
			
			System.out.println(pne.getMessage());
			
			ActionMessages errors = new ActionMessages();
			
			errors.add(
                    ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("pollinationNeeds.sql.exception"));
			
			this.saveErrors(request, errors);
			
			return mapping.findForward("failure");
			
		}
		
		
	}



}
